<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学生姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入学生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学籍号" prop="studentCode">
        <el-input
          v-model="queryParams.studentCode"
          placeholder="请输入学籍号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-input
          v-model="queryParams.gender"
          placeholder="请输入性别 0-保密 1-男 2-女"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="学生属性" prop="isDormitory">
        <el-input
          v-model="queryParams.isDormitory"
          placeholder="请输入是否住宿：0否1是"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wisdom:studentinfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wisdom:studentinfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wisdom:studentinfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wisdom:studentinfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studentinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学生ID" align="center" prop="id" />
      <el-table-column label="关联ID" align="center" prop="relationId" />
      <el-table-column label="学生姓名" align="center" prop="name" />
      <el-table-column label="学籍号" align="center" prop="studentCode" />
      <el-table-column label="身份证号" align="center" prop="idCard" />
      <el-table-column label="手机号码" align="center" prop="mobile" />
      <el-table-column label="性别" align="center" prop="gender" />
      <el-table-column label="出生日期" align="center" prop="birth" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birth, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="民族" align="center" prop="nation" />
      <el-table-column label="籍贯" align="center" prop="nativePlace" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="相片" align="center" prop="photo" />
      <el-table-column label="在校状态" align="center" prop="inSchool" />
      <el-table-column label="在校时间" align="center" prop="schoolTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.schoolTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="学生属性" align="center" prop="isDormitory" />
      <el-table-column label="住宿状态" align="center" prop="inDormitory" />
      <el-table-column label="宿舍时间" align="center" prop="dormitoryTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dormitoryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wisdom:studentinfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wisdom:studentinfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学生信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学校ID" prop="comId">
          <el-input v-model="form.comId" placeholder="请输入学校ID" />
        </el-form-item>
        <el-form-item label="关联ID" prop="relationId">
          <el-input v-model="form.relationId" placeholder="请输入关联ID" />
        </el-form-item>
        <el-form-item label="人脸id" prop="faceId">
          <el-input v-model="form.faceId" placeholder="请输入人脸id" />
        </el-form-item>
        <el-form-item label="学生姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入学生姓名" />
        </el-form-item>
        <el-form-item label="学籍号" prop="studentCode">
          <el-input v-model="form.studentCode" placeholder="请输入学籍号" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="手机号码" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="性别 0-保密 1-男 2-女" prop="gender">
          <el-input v-model="form.gender" placeholder="请输入性别 0-保密 1-男 2-女" />
        </el-form-item>
        <el-form-item label="出生日期" prop="birth">
          <el-date-picker clearable
            v-model="form.birth"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="民族" prop="nation">
          <el-input v-model="form.nation" placeholder="请输入民族" />
        </el-form-item>
        <el-form-item label="籍贯" prop="nativePlace">
          <el-input v-model="form.nativePlace" placeholder="请输入籍贯" />
        </el-form-item>
        <el-form-item label="家庭住址" prop="homeAddress">
          <el-input v-model="form.homeAddress" placeholder="请输入家庭住址" />
        </el-form-item>
        <el-form-item label="户口住址" prop="nodeAddress">
          <el-input v-model="form.nodeAddress" placeholder="请输入户口住址" />
        </el-form-item>
        <el-form-item label="是否有接种证 1-否 2-是" prop="isVaccine">
          <el-input v-model="form.isVaccine" placeholder="请输入是否有接种证 1-否 2-是" />
        </el-form-item>
        <el-form-item label="入学时间" prop="admissionTime">
          <el-date-picker clearable
            v-model="form.admissionTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择入学时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="毕业时间" prop="graduationTime">
          <el-date-picker clearable
            v-model="form.graduationTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择毕业时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="相片" prop="photo">
          <el-input v-model="form.photo" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="0未到校，1在校 2已离校" prop="inSchool">
          <el-input v-model="form.inSchool" placeholder="请输入0未到校，1在校 2已离校" />
        </el-form-item>
        <el-form-item label="在校发生时间" prop="schoolTime">
          <el-date-picker clearable
            v-model="form.schoolTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择在校发生时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否住宿：0否1是" prop="isDormitory">
          <el-input v-model="form.isDormitory" placeholder="请输入是否住宿：0否1是" />
        </el-form-item>
        <el-form-item label="是否在宿舍1 进宿舍 2出宿舍" prop="inDormitory">
          <el-input v-model="form.inDormitory" placeholder="请输入是否在宿舍1 进宿舍 2出宿舍" />
        </el-form-item>
        <el-form-item label="进出宿舍时间" prop="dormitoryTime">
          <el-date-picker clearable
            v-model="form.dormitoryTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择进出宿舍时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStudentinfo, getStudentinfo, delStudentinfo, addStudentinfo, updateStudentinfo } from "@/api/wisdom/studentinfo";

export default {
  name: "Studentinfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生信息表格数据
      studentinfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        comId: null,
        relationId: null,
        faceId: null,
        name: null,
        studentCode: null,
        idCard: null,
        mobile: null,
        gender: null,
        birth: null,
        nation: null,
        nativePlace: null,
        homeAddress: null,
        nodeType: null,
        nodeAddress: null,
        isVaccine: null,
        admissionTime: null,
        graduationTime: null,
        remarks: null,
        photo: null,
        status: null,
        inSchool: null,
        schoolTime: null,
        isDormitory: null,
        inDormitory: null,
        dormitoryTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        comId: [
          { required: true, message: "学校ID不能为空", trigger: "blur" }
        ],
        relationId: [
          { required: true, message: "关联ID不能为空", trigger: "blur" }
        ],
        faceId: [
          { required: true, message: "人脸id不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "学生姓名不能为空", trigger: "blur" }
        ],
        studentCode: [
          { required: true, message: "学籍号不能为空", trigger: "blur" }
        ],
        idCard: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ],
        gender: [
          { required: true, message: "性别 0-保密 1-男 2-女不能为空", trigger: "blur" }
        ],
        nodeType: [
          { required: true, message: "户口类型 1-农业 2-非农业不能为空", trigger: "change" }
        ],
        isVaccine: [
          { required: true, message: "是否有接种证 1-否 2-是不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "1-正常 2-毕业 退学-1 转校转学-2 休学-3不能为空", trigger: "change" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "最后更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询学生信息列表 */
    getList() {
      this.loading = true;
      listStudentinfo(this.queryParams).then(response => {
        this.studentinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        comId: null,
        relationId: null,
        faceId: null,
        name: null,
        studentCode: null,
        idCard: null,
        mobile: null,
        gender: null,
        birth: null,
        nation: null,
        nativePlace: null,
        homeAddress: null,
        nodeType: null,
        nodeAddress: null,
        isVaccine: null,
        admissionTime: null,
        graduationTime: null,
        remarks: null,
        photo: null,
        status: null,
        inSchool: null,
        schoolTime: null,
        isDormitory: null,
        inDormitory: null,
        dormitoryTime: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStudentinfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学生信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStudentinfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStudentinfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除学生信息编号为"' + ids + '"的数据项？').then(function() {
        return delStudentinfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wisdom/studentinfo/export', {
        ...this.queryParams
      }, `studentinfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
